package net.neptech.listener.impl;

import lombok.extern.slf4j.Slf4j;
import net.neptech.model.dto.InstanceAssignUserRecordParamDto;
import net.neptech.model.dto.InstanceExecutionDto;
import net.neptech.model.dto.MessageDto;
import net.neptech.enums.workflow.InstanceConstant;
import net.neptech.enums.workflow.MessageTypeEnum;
import net.neptech.listener.AbstractEventListenerStrategy;
import net.neptech.service.RemoteService;
import net.neptech.util.ExecutionUtil;
import net.neptech.util.FlowableUtil;
import net.neptech.util.JsonUtil;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 任务被设置执行人了
 */
@Slf4j
@Component
public class TaskAssignedEventListener extends AbstractEventListenerStrategy implements InitializingBean {
    @Autowired
    private RemoteService remoteService;
    /**
     * 处理数据
     */
    @Override
    public void handle(FlowableEvent event) {
        //任务被指派了人员
        FlowableEntityEvent flowableEntityEvent = (FlowableEntityEvent) event;
        TaskEntityImpl task = (TaskEntityImpl) flowableEntityEvent.getEntity();
        //执行人id
        String assignee = task.getAssignee();
        String executionId = task.getExecutionId();
        //查询父级执行id
        String parentExecutionId = ExecutionUtil.getParentExecutionId(executionId);
        //nodeid
        String nodeId = task.getTaskDefinitionKey();
        //实例id
        String processInstanceId = task.getProcessInstanceId();
        String processDefinitionId = task.getProcessDefinitionId();
        //流程id
        String flowId = FlowableUtil.getFlowId(processDefinitionId);
        InstanceAssignUserRecordParamDto instanceAssignUserRecordParamDto = new InstanceAssignUserRecordParamDto();
        instanceAssignUserRecordParamDto.setFlowId(flowId);
        instanceAssignUserRecordParamDto.setProcessInstanceId(processInstanceId);
        instanceAssignUserRecordParamDto.setNodeId(nodeId);
        instanceAssignUserRecordParamDto.setUserId((assignee));
        instanceAssignUserRecordParamDto.setTaskId(task.getId());
        instanceAssignUserRecordParamDto.setNodeName(task.getName());
        instanceAssignUserRecordParamDto.setFlowUniqueId(FlowableUtil.getFlowUniqueId(nodeId, flowId,
                processInstanceId, task.getTenantId()));
        String taskType = task.getVariableLocal(InstanceConstant.VariableKey.TASK_TYPE, String.class);
        instanceAssignUserRecordParamDto.setTaskType(taskType);
        instanceAssignUserRecordParamDto.setExecutionId(executionId);
        instanceAssignUserRecordParamDto.setParentExecutionId(parentExecutionId);
        remoteService.createTaskEvent(instanceAssignUserRecordParamDto);
        //保存执行信息
        InstanceExecutionDto instanceExecutionDto = new InstanceExecutionDto();
        instanceExecutionDto.setExecutionId(executionId);
        instanceExecutionDto.setParentIdExecutionId(parentExecutionId);
        instanceExecutionDto.setProcessInstanceId(processInstanceId);
        instanceExecutionDto.setNodeId(nodeId);
        instanceExecutionDto.setFlowId(flowId);
        remoteService.saveExecution(instanceExecutionDto);
        //待办消息
        //任务被创建了
        Map<String, Object> variables = task.getVariables();
        String taskId = task.getId();
        MessageDto messageDto = MessageDto.builder()
                .userId(assignee)
                .flowId(flowId)
                .processInstanceId(processInstanceId)
                .bizUniqueId(taskId)
                .param(JsonUtil.toJSONString(variables))
                .type(MessageTypeEnum.TODO_TASK.getType())
                .hasRead(false).build();
        remoteService.saveMessage(messageDto);
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        afterPropertiesSet(FlowableEngineEventType.TASK_ASSIGNED.toString());
    }
}
